<template>
<div v-if="getShow">
  <el-button v-bind="$attrs" v-on="$listeners" @click="buttonClick">{{fieldValue}}</el-button>
</div>

</template>

<script>
  import { commonGetInfo } from '@/api/common'
  import { mapGetters } from 'vuex'
  import { getParent} from '../utils'
  import analysisRouterParam from '@/utils/analysis-router-param.js'

  export default {
    props: {
      value: {
        type: [String,Number]
      },
    },
    data(){
      return {
        attrObj:{}
      }
    },
    computed: {
      ...mapGetters(['routerRules']),
      parent() {
        return getParent.call(this)
      },
      fieldValue: {
        get() {
          // console.log(this.$attrs,this.value,'--------------')
          return this.value ? this.value: '按钮';
        },
        set(val) {
          this.$emit('input', val)
        }
      },
      getShow(){
          var rules = this.routerRules;
          if(this.$attrs.url && rules.indexOf(this.$attrs.url.split('?')[0]) === -1) {
            return false;
          }
          this.attrObj = this.$attrs;

          return true;
      }
    },
    methods:{
        buttonClick(){
          var obj = this.attrObj;
          if(!obj.url) {
            return false;
          }
          const model = this.parent.currentModel;
          if(!model) {
            return false;
          }

          var routerUrl = analysisRouterParam.init(obj.url,model);
          
          switch (obj.changes) {
              case 'jump':
                  this.$router.push(routerUrl);
                  break;
              case 'blank':
                  const urlObj = this.$router.resolve(routerUrl);
                  window.open(urlObj.href,'_blank');
                  break;
              case 'alert':
                  // if(JSON.stringify(list) == '{}' || JSON.stringify(list) == '[]') {
                  //     this.$message({
                  //       message: '请先选择需要操作的数据',
                  //       type: 'error'
                  //     });
                  //     return false;
                  // }
                  // this.alertInit(Object.assign(routerUrl,obj));
                  break;
              case 'ajax':
                  if(obj.tips) {
                    this.$confirm(obj.tips, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.confirmAjax(routerUrl);
                    }).catch(() => {
                        console.log('取消')
                    });
                  }else{
                    this.confirmAjax(routerUrl);
                  }
                  break;
          }
        },
        async confirmAjax(route){
          const res = await commonGetInfo(route.path,route.query);
          
          if (res && res.code === 200) {
              this.$message({
                  type: 'success',
                  message: res.msg
              });
              this.$emit('refreshInit',{})
          }

        },
    }
  }
</script>


